<?php

/**
 * Description of Helper
 * 
 * @author anhducbkhn
 * @since Jul 20, 2013 - 5:25:25 PM
 */
class Helper
{

    public static $monthArray = array(
        1  => 'January',
        2  => 'February',
        3  => 'March',
        4  => 'April',
        5  => 'May',
        6  => 'June',
        7  => 'July',
        8  => 'August',
        9  => 'September',
        10 => 'October',
        11 => 'November',
        12 => 'December'
    );

    static function getTimeArray($slotMinutes = 15)
    {


        $start = strtotime('08:00');
        $end   = strtotime('21:00');
        for ($i = $start; $i <= $end; $i += 900)
        {
            $arr[date('H:i', $i)] = date('H:i', $i);
        }

        return $arr;
    }

    /**
     * 
     * @param type $length
     * @return random a string
     */
    static function generateRandomString($length = 10)
    {
        return substr(str_shuffle("0123456789"), 0, $length);
    }

    static function getGuikey($id)
    {
        $result = crc32($id);
        if ($result < 0)
        {
            $result = '0' . abs($result);
        }
        return sprintf("%12s", $result);
    }

    /*
     * Create ICS file for attachment via mail
     * @return link
     */

    public static function createICSFile($appoint, $customer)
    {
        //include
        $phpExcelPath = Yii::getPathOfAlias('application.extensions.IcsGen');
        include($phpExcelPath . DIRECTORY_SEPARATOR . 'invite.php');

        $path      = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'ics' . DIRECTORY_SEPARATOR;
        $file_name = $customer->user->email . '.ics';
        $subject   = $appoint->service->s_name;
        $invite    = new Invite();
        $invite
                ->setSubject($subject)
                ->setDescription($appoint->service->s_name)
                ->setStart(new DateTime("@" . Utils::convertToStamp($appoint->start_time, $appoint->date), new DateTimeZone('Asia/Singapore')))
                ->setEnd(new DateTime("@" . Utils::convertToStamp($appoint->end_time, $appoint->date), new DateTimeZone('Asia/Singapore')))
                ->setLocation($appoint->service->address)
                ->setOrganizer($customer->user->email, $customer->getName())
                ->addAttendee($appoint->author->email, $appoint->author->getName())
                ->generate() // generate the invite
                ->save($path, $file_name); // save it to a file

        $download_link = Invite::getSavedPath();
        return $download_link;
    }

    public static function getDayofMonth($month, $year)
    {
        if (isset($month) && isset($year) && $year > 0)
        {
            $month = intval($month);
            $year  = intval($year);
            if ($month <= 0 || $month > 12)
                return false;
            else
            {
                switch ($month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        return 31;
                        break;
                    case 2:
                        if ($year >= 4 && $year % 4 == 0)
                            return 29;
                        else
                            return 28;
                        break;
                    default:
                        return 30;
                        break;
                }
            }
        }
        else
            return false;
    }

    public static function getListYear($html = true, $empty = true, $emptyText = "")
    {
        $year = date('Y');
        if ($html == true)
        {
            $return = "";
            if ($empty == true)
                $return = '<option value="">:: ' . (($emptyText != "") ? $emptyText : Yii::t('main', 'Select')) . ' ::</option>';
            for ($i = $year; $i >= ($year - 10); $i--)
            {

                if ($return == '')
                {
                    $return = '<option value="' . $i . '">' . $i . '</option>';
                }
                else
                {
                    $return .= '<option value="' . $i . '">' . $i . '</option>';
                }
            }
        }
        else
        {
            $return    = array();
            if ($empty == true)
                $return[0] = ':: ' . ($emptyText != "") ? $emptyText : Yii::t('main', 'Select') . ' ::';
            for ($i = $year; $i >= ($year - 10); $i--)
            {
                $return[$i] = $i;
            }
        }
        return $return;
    }

    public static function getListMonth($year = 0, $html = true, $empty = true, $emptyText = "")
    {
        if (empty($year) || intval($year) <= 0)
            $year  = date('Y');
        if ($year == date('Y'))
            $month = intval(date('m'));
        else
            $month = 12;
        if ($html == true)
        {
            $return = "";
            if ($empty == true)
                $return = '<option value="">:: ' . (($emptyText != "") ? $emptyText : Yii::t('main', 'Select')) . ' ::</option>';
            for ($i = 1; $i <= $month; $i++)
            {

                if ($return == '')
                {
                    $return = '<option value="' . $i . '">' . self::$monthArray[$i] . '</option>';
                }
                else
                {
                    $return .= '<option value="' . $i . '">' . self::$monthArray[$i] . '</option>';
                }
            }
        }
        else
        {
            $return    = array();
            if ($empty == true)
                $return[0] = ':: ' . ($emptyText != "") ? $emptyText : Yii::t('main', 'Select') . ' ::';
            for ($i = 1; $i <= $month; $i++)
            {
                $return[$i] = self::$monthArray[$i];
            }
        }

        return $return;
    }

    public static function getListCycle($year = 0, $month = 0, $html = true, $empty = true, $emptyText = "")
    {
        if (empty($year) || intval($year) <= 0 || $year > (int) date('Y'))
            $year  = date('Y');
        if (empty($month) || intval($month) <= 0)
            $month = date('m');
        if ($year == date('Y') && $month > (int) date('m'))
            $month = intval(date('m'));
        if ($html == true)
        {
            $return = "";
            if ($empty == true)
                $return = '<option value="">:: ' . (($emptyText != "") ? $emptyText : Yii::t('main', 'Select')) . ' ::</option>';
            if ($year == date('Y') && $month == (int) date('m'))
            {
                $date = (int) date('d');
                if ($date <= 15)
                {
                    if ($return == "")
                        $return = '<option value="1">' . Yii::t('main', 'Cycle A') . '</option>';
                    else
                        $return .= '<option value="1">' . Yii::t('main', 'Cycle A') . '</option>';
                }else
                {
                    if ($return == "")
                        $return = '<option value="1">' . Yii::t('main', 'Cycle A') . '</option>';
                    else
                        $return .= '<option value="1">' . Yii::t('main', 'Cycle A') . '</option>';
                    $return .= '<option value="2">' . Yii::t('main', 'Cycle B') . '</option>';
                }
            }else
            {
                if ($return == "")
                    $return = '<option value="1">' . Yii::t('main', 'Cycle A') . '</option>';
                else
                    $return .= '<option value="1">' . Yii::t('main', 'Cycle A') . '</option>';
                $return .= '<option value="2">' . Yii::t('main', 'Cycle B') . '</option>';
            }
        }else
        {
            $return    = array();
            if ($empty == true)
                $return[0] = ':: ' . ($emptyText != "") ? $emptyText : Yii::t('main', 'Select') . ' ::';

            if ($empty == true)
                $return[0] = ':: ' . ($emptyText != "") ? $emptyText : Yii::t('main', 'Select') . ' ::';
            if ($year == date('Y') && $month == (int) date('m'))
            {
                $date = (int) date('d');
                if ($date <= 15)
                {
                    $return[1] = Yii::t('main', 'Cycle A');
                }
                else
                {
                    $return[1] = Yii::t('main', 'Cycle A');
                    $return[2] = Yii::t('main', 'Cycle B');
                }
            }
            else
            {
                $return[1] = Yii::t('main', 'Cycle A');
                $return[2] = Yii::t('main', 'Cycle B');
            }
        }

        return $return;
    }

}

/**
 * End of Helper
 */
